import Cookies from 'js-cookie'
import { remove } from 'nprogress'

const TokenKey = 'Token'
const UserIdKey = 'UserId'
const RoleIdKey = 'RoleId'
const AuthorityKey = 'OurCraft'

export function getToken() {
	let token = Cookies.get(TokenKey)
	return token == null ? localStorage.getItem(TokenKey) : token
}

export function getRoleId() {
	return sessionStorage.getItem(RoleIdKey)
}

export function getAuthority(){
	return sessionStorage.getItem(AuthorityKey)
}

export function getUserId() {
	return sessionStorage.getItem(UserIdKey)
}

export function setUser(data) {
	Cookies.set(TokenKey, data.token)
	localStorage.setItem(TokenKey, data.token)
	sessionStorage.setItem(UserIdKey, data.userId)
	sessionStorage.setItem(RoleIdKey, data.roleId)
	sessionStorage.setItem(AuthorityKey, data.authority)
}

export function removeUser() {
	Cookies.remove(TokenKey)
	localStorage.removeItem(TokenKey)
	sessionStorage.removeItem(UserIdKey)
	sessionStorage.removeItem(RoleIdKey)
	sessionStorage.removeItem(AuthorityKey)
}
